package com.hlhj.carview

import android.app.Application
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.hlhj.carview.utils.SPHelper
import com.lzy.okgo.OkGo
import com.lzy.okgo.cookie.CookieJarImpl
import com.lzy.okgo.cookie.store.MemoryCookieStore
import com.lzy.okgo.interceptor.HttpLoggingInterceptor
import com.lzy.okgo.model.HttpHeaders
import com.qiniu.android.storage.Configuration
import com.qiniu.android.storage.UploadManager
import com.tencent.smtt.sdk.QbSdk
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit
import java.util.logging.Level


/**
 * Created by Never Fear   on 2019\1\4 0004.
Never More....
 */
class MyApp :Application() {
    companion object{
        var myApp:MyApp?=null
        var upLoadManager:UploadManager?=null
    }
    override fun onCreate() {
        super.onCreate()
        myApp=this
        initQN()
        SDKInitializer.initialize(this);
        SDKInitializer.setCoordType(CoordType.BD09LL);
        /*Okgo设置*/
        OkGo.getInstance().init(this)
        val headers = HttpHeaders()
        headers.put("Authorization","Bearer ${SPHelper.getToken(this)}")
        OkGo.getInstance().addCommonHeaders(headers)

        val builder = OkHttpClient.Builder()
        val loggingInterceptor = HttpLoggingInterceptor("OkGo")
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY)
        loggingInterceptor.setColorLevel(Level.INFO)
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS)
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS)
        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS)
        builder.cookieJar(CookieJarImpl(MemoryCookieStore()))
        builder.addInterceptor(loggingInterceptor)
        OkGo.getInstance().okHttpClient = builder.build()

        QbSdk.initX5Environment(this,object : QbSdk.PreInitCallback{
            override fun onCoreInitFinished() {

            }

            override fun onViewInitFinished(p0: Boolean) {

            }
        })
    }

    private fun initQN() {
        val config: Configuration = Configuration.Builder()
                .connectTimeout(90) // 链接超时。默认90秒
                .useHttps(true) // 是否使用https上传域名
                .useConcurrentResumeUpload(true) // 使用并发上传，使用并发上传时，除最后一块大小不定外，其余每个块大小固定为4M，
                .concurrentTaskCount(3) // 并发上传线程数量为3
                .responseTimeout(90) // 服务器响应超时。默认90秒
                .build()

        upLoadManager = UploadManager(config)
    }
}